package com.onLineCar.amap.config;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;

import com.onLineCar.amap.domain.AMapAddressData;
import com.onLineCar.amap.domain.AMapRouteData;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @program: onLineCar-server
 * @description:
 * @author: godGuang
 * @create: 2023-12-13 15:58
 **/
@Component
@Slf4j
public class AMapApi {
    @Value("${aMap.key}")
    String key;

    /**


     * 根据经纬度获取地址
     * @param location
     * @return
     */
    public AMapAddressData getAddressByLocation(String location) {
        String url = "https://restapi.amap.com/v3/geocode/regeo?output=JSON&extensions=base&location=" + location + "&key=" + key;
        log.info(url);
        HttpResponse response = HttpUtil.createGet(url).execute();
        if (null == response) {
            throw new RuntimeException("调用失败");
        }
        if (response.getStatus()!= 200) {
            throw new RuntimeException("调用错误");
        }
        return JSONObject.parseObject(response.body(), AMapAddressData.class);
    }

    /**

     * 获取路线API
     * @param origin
     * @param destination
     * @return
     */


    public AMapRouteData getRoute(String origin, String destination) {

        String url = "https://restapi.amap.com/v3/direction/driving?origin=" + origin + "&destination=" + destination + "&extensions=all&key=" + key;
        log.info(url);
        HttpResponse response = HttpUtil.createGet(url).execute();
        if (null == response) {
            System.out.println("调用失败");
        }

        return JSONObject.parseObject(response.body(), AMapRouteData.class);

    }
}

